//
//  VL4WiFiCell.m
//  DoorBell
//
//  Created by Chun Hung Cheung on 2022/12/27.
//  Copyright © 2022 Chun Hung Cheung. All rights reserved.
//

#import "VL4WiFiCell.h"

@interface VL4WiFiCell()
@property(nonatomic,strong)UIImageView *wifi_icon;
@property(nonatomic,strong)UILabel *wifi_name;
@property(nonatomic,strong)UIImageView *good_icon;

@property(nonatomic,strong)UILabel *titleLabel;

@end

@implementation VL4WiFiCell

- (void)awakeFromNib {
    [super awakeFromNib];
   
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

  
}


- (UIImageView*)selectedImage
{
    // 勾选框
    if (!_selectedImage) {
         _selectedImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_check_alpha"]];
        [self.contentView addSubview:_selectedImage];
        [_selectedImage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.right.equalTo(self.contentView.mas_right).offset(-20);
            make.width.mas_equalTo(20);
            make.height.mas_equalTo(20);
        }];
    }
    
    return _selectedImage;
}

- (UIImageView*)wifi_icon
{
    if (!_wifi_icon) {
         _wifi_icon = [[UIImageView alloc]init];
        [self.contentView addSubview:_wifi_icon];
        [_wifi_icon mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(22);
            make.height.mas_equalTo(16);
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.left.equalTo(self.contentView.mas_left).offset(14);
        }];
    }
    return _wifi_icon;
}

- (UILabel*)wifi_name
{
    if (!_wifi_name) {
         _wifi_name = [[UILabel alloc]init];
        [self.contentView addSubview:_wifi_name];
         _wifi_name.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
         _wifi_name.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16.0];
         _wifi_name.numberOfLines = 0;
         _wifi_name.textAlignment = NSTextAlignmentLeft;
        [_wifi_name sizeToFit];
        [_wifi_name mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(41);
            make.centerY.equalTo(self.contentView.mas_centerY);
        }];
   }
    return _wifi_name;
}



- (UIImageView*)good_icon
{
    if (!_good_icon) {
         _good_icon = [[UIImageView alloc]init];
        [self.contentView addSubview:_good_icon];
        [_good_icon mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(13);
            make.height.mas_equalTo(14);
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.left.equalTo(self.wifi_name.mas_right).offset(14);
        }];
    }
    return _good_icon;
}

- (void)setWifi_dict:(NSDictionary *)wifi_dict
{
    _wifi_dict = wifi_dict;
    self.wifi_icon.image = [self getimage:abs([[wifi_dict objectForKey:@"rssi"] intValue])];
    self.wifi_name.text = [wifi_dict objectForKey:@"ssid"];
    
    int rssi = abs([[wifi_dict objectForKey:@"rssi"] intValue]);
    NSLog(@"rssi-->%d",rssi);
    
    if(rssi <= 55){
        self.good_icon.image = [UIImage imageNamed:@"vl4_recommend"];
    }else{
        if(_good_icon){
         [_good_icon removeFromSuperview];
          _good_icon = nil;
        }
    }
}


- (UIImage*)getimage:(int)wifirssi
{
    UIImage *image;
    if(wifirssi<= 55){
        image = [UIImage imageNamed:@"vl4_wifi4"];
    }else if(55 < wifirssi && wifirssi <= 70){
        image = [UIImage imageNamed:@"vl4_wifi3"];
    }else if(70 < wifirssi && wifirssi <= 85){
        image = [UIImage imageNamed:@"vl4_wifi2"];
    }else if(wifirssi > 85){
        image = [UIImage imageNamed:@"vl4_wifi1"];
    }
    
    return image;
}


- (void)setList:(FBDevicesList *)list
{
     _list = list;

    [_wifi_name mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(41);
        make.centerY.equalTo(self.contentView.mas_centerY);
    }];
    self.wifi_name.text = [NSString stringWithFormat:@"昵称:%@ \n设备Mac:%@",list.nick,list.name];
    self.selectedImage.image = [UIImage imageNamed:@"icon_check_alpha"];
}

- (UILabel*)titleLabel
{
    if (!_titleLabel) {
         _titleLabel = [[UILabel alloc]init];
        [self.contentView addSubview:_titleLabel];
        _titleLabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
        _titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16.0];
        _titleLabel.numberOfLines = 0;
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        [_titleLabel sizeToFit];
        [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(20);
            make.centerY.equalTo(self.contentView.mas_centerY);
        }];
   }
    return _titleLabel;
}

- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLabel.text = title;
    self.selectedImage.image = [UIImage imageNamed:@"icon_check_alpha"];
}


@end
